package au.com.somethingwritten.controller;

import au.com.somethingwritten.model.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class BookController extends HttpServlet {

	public void doPost( HttpServletRequest request,
						HttpServletResponse response)
						throws IOException, ServletException {
		doGet(request, response);
	}
	
	public void doGet( HttpServletRequest request,
			HttpServletResponse response)
			throws IOException, ServletException {
			
		//get the requested book's id
		int bookId;
		if(request.getParameter("book") == null){
			bookId = 1;
		}else{
			bookId = Integer.parseInt((String)request.getParameter("book"));
			
		}
		//create a new book manager
		BookManager bookMan = new BookManager();
		//get the requested book
		Book book = bookMan.getBook(bookId);
		//store book in the request
		request.setAttribute("resultBook", book);
		//forward data to the view
		RequestDispatcher view = request.getRequestDispatcher("bookPage.jsp");
		view.forward(request, response); 
	}
}
